package cn.lu.train.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import cn.lu.train.ui.activity.ScannerActivity;
import me.barcodescanner.util.ZBarHelper;
import me.dm7.barcodescanner.zbar.Result;

public class ScanHelper {

    private static OnScanListener listener;

    public static void startScan(Context context, OnScanListener listener) {
        startScanActivity(context, 0, listener);
    }

    public static void startBarScan(Context context, OnScanListener listener) {
        startScanActivity(context, ZBarHelper.CODE_TYPE_ONE_D, listener);
    }

    public static void startQrScan(Context context, OnScanListener listener) {
        startScanActivity(context, ZBarHelper.CODE_TYPE_QR_CODE, listener);
    }

    private static void startScanActivity(Context context, int type, OnScanListener listener) {
        ScanHelper.listener = listener;
        Intent intent = new Intent(context, ScannerActivity.class);
        intent.putExtra("type", type);
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    public static OnScanListener listener() {
        return listener;
    }

    public interface OnScanListener{
        void onResult(Result result);
    }
}
